package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ImportErpWorkOrderDetail;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseErpIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseErpOrderListVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.Result;
import com.zmj.sy.mom.srv.aps.bean.vo.importerpmapping.ErpWorkOrderDetailResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderErpMaintainVo;
import com.zmj.sy.mom.srv.aps.mapper.ImportErpWorkOrderDetailMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class ErpWorkOrderDetailService {

    private final ImportErpWorkOrderDetailMapper importErpWorkOrderDetailMapper;

    public BasePageResVo<ErpWorkOrderDetailResVo> getErpOrder(BaseErpOrderListVo orderListVo) {
        LambdaQueryWrapper<ImportErpWorkOrderDetail> lqw = new LambdaQueryWrapper<ImportErpWorkOrderDetail>();
        lqw.like(!StringUtils.isBlank(orderListVo.getOrderCode()), ImportErpWorkOrderDetail::getOrderCode, orderListVo.getOrderCode());
        lqw.like(!StringUtils.isBlank(orderListVo.getWorkOrderCode()), ImportErpWorkOrderDetail::getWorkOrderCode, orderListVo.getWorkOrderCode());
        lqw.like(!StringUtils.isBlank(orderListVo.getMaterialCode()), ImportErpWorkOrderDetail::getMaterialCode, orderListVo.getMaterialCode());
        Page<ImportErpWorkOrderDetail> importErpWorkOrderDetailPage = importErpWorkOrderDetailMapper.selectPage(orderListVo.toPage(), lqw);
        return BasePageResVo.of(importErpWorkOrderDetailPage, ErpWorkOrderDetailResVo.class);
    }

    public ImportErpWorkOrderDetail getOrderById(BaseErpIdReqVo reqVo) {
        return importErpWorkOrderDetailMapper.selectById(reqVo.getId());
    }

    public Result<String> editOrder(OrderErpMaintainVo orderErpMaintainVo) {
        ImportErpWorkOrderDetail selectById = importErpWorkOrderDetailMapper.selectById(orderErpMaintainVo.getId());
        BeanUtils.copyProperties(orderErpMaintainVo,selectById);
        importErpWorkOrderDetailMapper.updateById(selectById);
        return Result.success();
    }

}
